<template>
  <div>
    <div class="demo-upload-list" v-for="item in imgList">
      <template v-if="item.status === 'finished'">
          <img :src="`${item.response.res.requestUrls}`">
          <div class="demo-upload-list-cover">
              <!-- <Icon type="ios-camera" @click.native="handleUpload(item)"></Icon> -->
              <Icon type="ios-eye-outline" @click.native="handleView(item.response.res.requestUrls)"></Icon>
              <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
          </div>
      </template>
      <template v-else>
          <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
  </div>
    <Uploadoss
      ref="uploadoss"
      :show-upload-list="false"
      :format="['jpg', 'jpeg', 'png']"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :on-success="handleSuccess"
      multiple
      :before-upload="handleBeforeUpload"
      type="drag"
      style="display: inline-block; width: 58px;"
    >
      <div style="width: 58px; height: 58px; line-height: 58px;">
        <Icon type="ios-camera" size="20"></Icon>
      </div>    
    </Uploadoss>
    <Modal title="原图" v-model="visible">
        <img :src="`${imgName}`" v-if="visible" style="width: 100%">
    </Modal>
  </div>
</template>
<script>
import Uploadoss from '@/components/uploadoss'  
import fetch from '@/utils/fetch'
import moment from 'moment'
export default {
  name: 'upload-image',
  components: { Uploadoss },
  props: {
    css: {
      type: String,
      default: 'width:300px;'
    },
    uploadList: {
      type: Array,
      default () {
        return []
      }
    },
    imgSize: {
      type: Number,
      default: 5
    }
  },
  data () {
    return {
      visible: false,
      imgName: '',
      imgList: this.uploadList
    }
  },
  methods: {
    moment,
    handleFormatError (file) {
      this.$Notice.warning({
        title: '文件格式不对',
        desc: 'File format of ' + file.name + ' is incorrect, please select jpg or png.'
      })
    },
    handleMaxSize (file) {
      this.$Notice.warning({
        title: '超出大小限制',
        desc: 'File  ' + file.name + ' 太大, 不能超过 2M.'
      })
    },
    handleBeforeUpload (file) {
      if (this.imgList.length >= this.imgSize) {
        this.$Notice.warning({
          title: '图片数量限制'
        })
        return false
      };
      return fetch({
        url: process.env.VUE_APP_UPLOAD_URL,
        method: 'POST',
        data: { 'exchangeTime': moment().format('YYYY-MM-DD HH:mm:ss'),
          'channelId': '1',
          'channelNo': '1',
          'channelPassword': '1',
          'appVersion': '1',
          'appMobileModel': '1'
        } })
    },
    handleSuccess () {
      this.$emit('update:uploadList', this.imgList)
      this.$emit('imageupload')
    },
    handleView (name) {
      this.imgName = name
      this.visible = true
    },
    handleRemove (file) {
      console.log(file,'deldeldeldeldeldeldeldeldeldeldeldeldeldeldeldel')
      const fileList = this.$refs.uploadoss.fileList
      this.$refs.uploadoss.fileList.splice(fileList.indexOf(file), 1)
    },
    // handleUpload (file) {

    // }
  },
  mounted () {
    this.imgList = this.$refs.uploadoss.fileList

    
  },
  watch: {
    uploadList: {
      handler (val) {
        this.imgList = val
        this.$refs.uploadoss.fileList = val
        // let tempList = [];
        // val.forEach((element) => {
        //   console.log("element:"+element);
        //   tempList.push(element.url);
        // });
        // this.$emit('imageupload');
      },
      deep: true
    },
    'mutileUploadData.defaultList': {
      handler (val) {
        this.imgList.splice(0, this.imgList.length) // 清空数组
        val.forEach((element) => {
          this.imgList.push({
            name: element.name,
            url: element.url,
            showProgress: false,
            status: 'finished'
          })
        })
      },
      deep: true
    }
  }
}
</script>
<style>
.demo-upload-list{
    display: inline-block;
    width: 90px;
    height: 90px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
}
.demo-upload-list img{
    width: 100%;
    height: 100%;
}
.demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
}
.demo-upload-list:hover .demo-upload-list-cover{
    display: block;
}
.demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
}
</style>
